﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WC.BL.BCC;
using WC.BL.BE;

namespace WC.UI.WebApp
{
    public partial class registro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.cargarCombos();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string retval = string.Empty;
            string validaRuc = string.Empty;
            ClienteBE objClienteBE = new ClienteBE();
            try
            {
                if (string.IsNullOrEmpty(txtRuc.Text.Trim()) || string.IsNullOrEmpty(txtNombre.Text.Trim()) ||
                    string.IsNullOrEmpty(ddlDepartamento.SelectedValue) || string.IsNullOrEmpty(ddlProvincia.SelectedValue) ||
                    string.IsNullOrEmpty(ddlDistrito.SelectedValue) || string.IsNullOrEmpty(ddlTienda.SelectedValue) ||
                    string.IsNullOrEmpty(txtRepLegal.Text.Trim()) || string.IsNullOrEmpty(txtDniRepLegal.Text.Trim()))
                {
                    throw new Exception("Debe completar todos los campos obligatorios.");
                }                    

                objClienteBE.Ruc = txtRuc.Text.Trim();
                objClienteBE.Nombre = txtNombre.Text.Trim();
                objClienteBE.Correo = txtCorreo.Text.Trim();
                objClienteBE.Direccion = txtDireccion.Text.Trim();
                objClienteBE.Distrito = ddlDistrito.SelectedValue;
                objClienteBE.Provincia = ddlProvincia.SelectedValue;
                objClienteBE.Departamento = ddlDepartamento.SelectedValue;

                objClienteBE.Telefono = txtTelefono.Text.Trim();
                objClienteBE.Fax = txtFax.Text.Trim();
                objClienteBE.Ramo = ddlRamo.SelectedValue;
                objClienteBE.Tienda = ddlTienda.SelectedValue;
                objClienteBE.RepLegal = txtRepLegal.Text.Trim();
                objClienteBE.RepLegalDNI = txtDniRepLegal.Text.Trim();
                objClienteBE.RespCompras = txtRespCompras.Text.Trim();

                validaRuc = ClienteBC.validaCli(txtRuc.Text.Trim());
                if (!string.IsNullOrEmpty(validaRuc))
                {
                    retval = validaRuc;
                }
                else
                {
                    retval = ClienteBC.write(objClienteBE);
                    limpiarTextos();
                }
            }
            catch (Exception ex)
            {
                retval = ex.Message;
            }
            lblErrorMsg.Text = retval.Trim();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("login.aspx");
        }

        private void cargarCombos()
        {
            try
            {
                ddlTienda.DataSource = TiendaBC.list();
                ddlTienda.DataBind();
                ddlTienda.SelectedValue = string.Empty;

                ddlRamo.DataSource = SectorBC.list();
                ddlRamo.DataBind();

                ddlDepartamento.DataSource = HelperBC.getDptos();
                ddlDepartamento.DataBind();

                try
                {
                    ddlDepartamento.SelectedValue = "06";
                }
                catch { }

                ddlProvincia.DataSource = HelperBC.getProv(ddlDepartamento.SelectedValue);//Lima y Callao
                ddlProvincia.DataBind();

                try
                {
                    ddlProvincia.SelectedValue = "01";//Lima
                }
                catch { }

                ddlDistrito.DataSource = HelperBC.getDist(ddlDepartamento.SelectedValue, ddlProvincia.SelectedValue);
                ddlDistrito.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void limpiarTextos()
        {
            txtRuc.Text = string.Empty;
            txtNombre.Text = string.Empty;
            txtCorreo.Text = string.Empty;
            txtDireccion.Text = string.Empty;
            ddlDistrito.SelectedIndex = 0;
            ddlProvincia.SelectedIndex = 0;
            ddlDepartamento.SelectedIndex = 0;

            txtTelefono.Text = string.Empty;
            txtFax.Text = string.Empty;
            ddlRamo.SelectedIndex = 0;
            ddlTienda.SelectedIndex = 0;
            txtRepLegal.Text = string.Empty;
            txtDniRepLegal.Text = string.Empty;
            txtRespCompras.Text = string.Empty;
            lblErrorMsg.Text = string.Empty;
        }

        protected void ddlDepartamento_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ddlProvincia.DataSource = HelperBC.getProv(ddlDepartamento.SelectedValue);//Lima y Callao
                ddlProvincia.DataBind();

                ddlDistrito.DataSource = HelperBC.getDist(ddlDepartamento.SelectedValue, ddlProvincia.SelectedValue);
                ddlDistrito.DataBind();
            }
            catch
            {

            }
        }

        protected void ddlProvincia_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ddlDistrito.DataSource = HelperBC.getDist(ddlDepartamento.SelectedValue, ddlProvincia.SelectedValue);
                ddlDistrito.DataBind();
            }
            catch
            {

            }
            
        }

        protected void ddlDistrito_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
        }

        
    }
}
